package com.bosssoft.goods.controller;

import com.bosssoft.base.entity.Result;
import com.bosssoft.goods.domain.vo.AddSpecVO;
import com.bosssoft.goods.domain.vo.EditSpecVO;
import com.bosssoft.goods.service.GoodsSpecificationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 商品规格参数设置API接口
 * @author Fang_Sir
 * @date 2021-04-16
 */
@RestController
@RequestMapping("/manage/spec")
@Api(tags = "3-商品规格参数管理")
public class GoodsSpecificationController {
    @Autowired
    private GoodsSpecificationService specificationService;

    @GetMapping("/getCatSpec/{catId}")
    @ApiOperation(value = "获取分类商品规格")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "catId", value = "第三级分类编号", required = true, dataType = "int",paramType = "path")
    })
    public Result<?> getCatSpec(@PathVariable("catId")int catId) {
        return Result.ok(specificationService.getCatSpec(catId));
    }

    @PostMapping("/newCatSpec")
    @ApiOperation(value = "新增分类商品规格")
    public Result<?> addSpec(@Valid @RequestBody AddSpecVO spec, BindingResult result) {
        if(result.hasErrors()){
            return Result.error(result.getAllErrors().get(0).getDefaultMessage());
        }
        try {
            return Result.ok(specificationService.addSpec(spec));
        }catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @PutMapping("/editCatSpec")
    @ApiOperation(value = "更改分类商品规格名称")
    public Result<?> editSpec(@Valid @RequestBody EditSpecVO spec, BindingResult result) {
        if(result.hasErrors()){
            return Result.error(result.getAllErrors().get(0).getDefaultMessage());
        }
        try {
            return specificationService.editSpec(spec) ? Result.ok() : Result.error("规格参数编号或第三级分类编号错误");
        }catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @DeleteMapping("/delCatSpec/{propId}")
    @ApiOperation(value = "删除分类商品规格参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "propId", value = "规格参数编号", required = true, dataType = "int",paramType = "path")
    })
    public Result<?> delSpec(@PathVariable("propId")int propId) {
        return specificationService.delSpec(propId) ? Result.ok() : Result.error();
    }
}
